package com.aftersale.demo.service.impl;

import com.aftersale.demo.entity.Project;
import com.aftersale.demo.entity.Result;
import com.aftersale.demo.entity.Solve;
import com.aftersale.demo.mapper.ProjectMapper;
import com.aftersale.demo.mapper.SolveMapper;
import com.aftersale.demo.mapper.UserMapper;
import com.aftersale.demo.utils.FileUpload;
import com.aftersale.demo.utils.MyUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aftersale.demo.entity.Problem;
import com.aftersale.demo.service.ProblemService;
import com.aftersale.demo.mapper.ProblemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
*@Author  空
*@Date    2023/10/23 20:16
*@Desc
*/
@Service
public class ProblemServiceImpl extends ServiceImpl<ProblemMapper, Problem>
    implements ProblemService{
    @Autowired
    ProblemMapper problemMapper;
    @Autowired
    ProjectMapper projectMapper;
    @Autowired
    SolveMapper solveMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public Result updateProblemState(int problemId, String state) {
        problemMapper.updatestate(problemId,state,MyUtil.getNow());
        return Result.success("修改状态成功");
    }

    @Override
    public Result updateProblemEvaluate(int problemId, Integer evaluate) {
        if(evaluate>10||evaluate<0){
            return Result.error("401","评价应为0-10的数字");
        }
        problemMapper.updatevaluate(problemId,evaluate,MyUtil.getNow());
        return Result.success("评价成功");
    }

    @Override
    public Result deleteProblem(int problemId) {
        Problem dbProblem =problemMapper.selectById(problemId);
        if(Objects.isNull(dbProblem)){
            return Result.error("401","问题不存在");
        }else{
            problemMapper.deleteById(problemId);
            return Result.success("删除成功");
        }

    }

    @Override
    public Result addProblem(Problem problem) {

        if(!Objects.isNull(problem.getFile())){
            if(!problem.getFile().isEmpty()){
                String url = FileUpload.upload(problem.getFile());
                problem.setUrl(url);
            }
        }
        problem.setCreateDate(MyUtil.getNow());
        problem.setState("待处理");
        problemMapper.insert(problem);
        int projectId=problem.getProjectId();
        userMapper.updateRemind(projectId);
        return Result.success("提交成功");
    }

    @Override
    public Result  getProblem(int userId, Integer pageNum, Integer pageSize) {
        List<Integer> projectIds=problemMapper.getProject(userId);
        if(projectIds.isEmpty()){
            return Result.error("401","您还没有客户");
        }
        QueryWrapper<Problem> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("project_id",projectIds);
        Page<Problem> problemPage=page(new Page<>(pageNum,pageSize),queryWrapper);
        if(Objects.isNull(problemPage)){
            return Result.error("401","您的客户还未提过问题");
        }else{
            for (Problem problem : problemPage.getRecords()) {
                problem.setName(projectMapper.selectById(problem.getProjectId()).getName());
            }
            return Result.success("获取成功",problemPage);
        }
    }

    @Override
    public Result updateProblem(Problem problem) {
        problem.setUpdateTime(MyUtil.getNow());
        if(!Objects.isNull(problem.getFile())){
            if(!problem.getFile().isEmpty()){
                String url = FileUpload.upload(problem.getFile());
                problem.setUrl(url);
            }
        }
        problemMapper.updateById(problem);
        return Result.success("修改成功");
    }

    @Override
    public Result problems(int userId,int roleId, Integer pageNum, Integer pageSize) {
        Page<Problem> problemPage;
        Page mPage=new Page<>(pageNum,pageSize);
        if(roleId==1||roleId==2){
            problemPage=page(mPage);
        }else {
            LambdaQueryWrapper<Problem> problemQueryWrapper=new LambdaQueryWrapper<>();
            problemQueryWrapper.eq(Problem::getUserId,userId);
            problemPage=page(mPage,problemQueryWrapper);
            for (Problem problem : problemPage.getRecords()) {
               QueryWrapper<Solve> queryWrapper=new QueryWrapper<>();
               queryWrapper.eq("problem_id",problem.getId());
               problem.setSolve(solveMapper.selectOne(queryWrapper));
            }
        }
        List<Problem> problemList=problemPage.getRecords();
        for (Problem problem : problemList) {
            problem.setName(projectMapper.selectById(problem.getProjectId()).getName());
        }
        return Result.success("",problemPage);
    }

    @Override
    public Result selectProblem(int userId, String name,Integer pageNum, Integer pageSize) {
        QueryWrapper<Problem> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.like("problem_description",name);
        Page<Problem> problemPage=page(new Page<>(pageNum,pageSize),queryWrapper);
        List<Problem> problemList=problemPage.getRecords();
        for (Problem problem : problemList) {
            problem.setName(projectMapper.selectById(problem.getProjectId()).getName());
        }
        return Result.success(problemPage);
    }
}




